/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.social;

import com.nc.kyiv.yellow.sociallibrary.POJO.LibUser;
import java.util.Properties;
import com.nc.kyiv.yellow.sociallibrary.dao.UserDAO;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author My
 */
public class Facebook extends OAuthClient {

    final String CLIENT_ID = "496353843797019";
    final String CLIENT_SECRET = "7adfa0df3f672a164842626bee661a69";
    final String REDIRECT_URI = "http://localhost:8080/Social_Library/Facebook";
    final String MAIN_PAGE_URL = "http://localhost:8080/Social_Library";
    final String URL_BEGINNING_FOR_ACCESS_TOKEN = "https://graph.facebook.com/oauth/access_token";
    final String URL_BEGINNIG_FOR_PERSONAL_DATA = "https://graph.facebook.com/me?access_token=";
    final String BIRTHDAY_DATE_FORMAT = "MM/dd/yyyy";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String personalData = getPersonalData(req, URL_BEGINNING_FOR_ACCESS_TOKEN, CLIENT_ID,
                CLIENT_SECRET, REDIRECT_URI, URL_BEGINNIG_FOR_PERSONAL_DATA);
        
        Map<String, String> personalDataMap = parseJSON(personalData);

        LibUser user = new LibUser("111", personalDataMap.get("id"), personalDataMap.get("first_name"),
                personalDataMap.get("last_name"), personalDataMap.get("email"));
        if (personalDataMap.get("gender") != null) {
            switch (personalDataMap.get("gender")) {
                case "male":
                    user.setGender(2);
                    break;
                case "female":
                    user.setGender(1);
                    break;
                default:
                    user.setGender(0);
                    break;
            }
        }
        if (personalDataMap.get("birthday") != null) {
            String pureBirthday = personalDataMap.get("birthday").replace("\\", "");
            Date birthday = getUnifiedBirthDate(pureBirthday, BIRTHDAY_DATE_FORMAT);
            user.setBirthDate(birthday);
        }
        
        save(user);
        login(user, req);
        resp.sendRedirect(MAIN_PAGE_URL);

    }

    @Override
    protected String getPureAccessToken(String urlForAccessToken) {
        String rawAccessToken;
        try {
            rawAccessToken = getStringFromHTMLwithGetMethod(urlForAccessToken);
            return rawAccessToken.split("access_token=")[1].split("&")[0];
        } catch (IOException ex) {
            System.out.println("Cant obtain facebook access token");
        }
        return null;
    }
}
